<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['types']=array(
	'1'=>'普通防伪码',
	'2'=>'分节防伪码',
	'3'=>'防伪二维码',
);
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['goods_id']){
		$params['goods_id']=$_RQ['goods_id'];
		$return['params']['goods_id']=$_RQ['goods_id'];
	}
	if($_RQ['code']){
		$params['code like']='%'.trim($_RQ['code']).'%';
		$return['params']['code']=$_RQ['code'];
	}
	//批号
	if(isset($_RQ['log_status'])){
		$params['log_status']=trim($_RQ['log_status']);
		$return['params']['log_status']=$_RQ['log_status'];
	}
	//备注
	if(!empty($_RQ['sn'])){
		$_RQ['sn']=str_replace('FW','',$_RQ['sn']);
		$_RQ['sn']=str_replace('XW','',$_RQ['sn']);
		$params['sn']=intval($_RQ['sn']);
		$return['params']['sn']=$_RQ['sn'];
	}
    $fields=array();
    $orderby='id desc';
	$total='';
	$list=pdo_getslice('jy_goodserpfw_xcode',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$item){
		$item['beinum']=$item['endnum']-$item['startnum']+1;
		$item['sn']=sprintf("%08d", $item['sn']);
		$item['startnum']=sprintf("%08d", $item['startnum']);
		$item['endnum']=sprintf("%08d", $item['endnum']);
		$item['pre']=pdo_getcolumn('jy_goodserpfw_batch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['batch_id']),'xpre');
		$item['presn']=$item['pre'].$item['sn'];
		$kulog=pdo_get('jy_goodserpfw_kulog',array('code'=>$item['code'],'uniacid'=>$_SESSION['uniacid']),'','createtime desc');
		$item['kutime']=$kulog['createtime'];
		$item['iscode']=pdo_count('jy_goodserpfw_code',array('xiang_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['status']=$this->code_status;		
	$return['log_status']=$this->codelog_status;		
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'箱码管理'),
		),
		'list'=>array(
		'tip'=>'防伪的<em>业务逻辑</em>：',
		'data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'pre','title'=>'前缀','type'=>'string'),
			array('name'=>'sn','title'=>'流水号','type'=>'string'),
			array('name'=>'presn','title'=>'带前缀流水号','type'=>'string'),
			array('name'=>'iscode','title'=>'已拖数','type'=>'string'),
			//array('name'=>'startnum','title'=>'小标签开始','type'=>'string'),
			//array('name'=>'endnum','title'=>'小标签结尾','type'=>'string'),
			array('name'=>'code','title'=>'防伪码','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'log_status','title'=>'流通状态','type'=>'radio','options'=>'log_status'),
			array('name'=>'kutime','title'=>'最近流通时间','type'=>'time'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','op'=>'upxcode','title'=>'清空该箱','show'=>array('log_status'=>array(0,1),'iscode >'=>0)),
			array('type'=>'modal','op'=>'upstatus1','title'=>'恢复未出库状态','show'=>array('log_status'=>array(2,3))),
		)),
		'search'=>array(
			array('name'=>'log_status','width'=>15,'title'=>'流通状态','type'=>'radio','options'=>'log_status'),
			array('name'=>'sn','width'=>15,'title'=>'流水号','type'=>'string'),
			array('name'=>'code','width'=>15,'title'=>'防伪码','type'=>'string'),
			array('name'=>'goods_id','width'=>15,'title'=>'所属商品','type'=>'choose','action'=>'jy_goodserpfw/get_goods/'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='upxcode'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$num=intval();
		$basic=$this->plugin_setting('basic');
		if(!$basic['mima']){
			exi('请先设置【操作密码】（基本设置--防伪码生成设置）！','error');
		}
		if(!$postdata['mima']){
			exi('请先输入【操作密码】！','error');
		}
		if($postdata['mima']!=$basic['mima'].'123'){
			exi('输入的【操作密码】不对！','error');
		}
		$id=$postdata['id'];
		pdo_update('jy_goodserpfw_code',array('log_status'=>0,'xiang_id'=>0),array('xiang_id'=>$postdata['id'],'uniacid'=>$_SESSION['uniacid']));
		pdo_update('jy_goodserpfw_xcode',array('log_status'=>0),array('id'=>$postdata['id'],'uniacid'=>$_SESSION['uniacid']));
		exi('操作成功！','error','/xiang/list');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择箱！','error');
	}
	$item=pdo_get('jy_goodserpfw_xcode',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'',
		'title'=>'恢复箱',
		'edit'=>array(
			array('title'=>'【温馨提示】：本操作会同时更新本箱的所有盒标签未未入库状态','type'=>'tip'),
			array('name'=>'mima','title'=>'操作密码','type'=>'string','help'=>'为了安全，必须输入操作密码，才能操作'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='upstatus1'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$num=intval();
		$basic=$this->plugin_setting('basic');
		if(!$basic['mima']){
			exi('请先设置【操作密码】（基本设置--防伪码生成设置）！','error');
		}
		if(!$postdata['mima']){
			exi('请先输入【操作密码】！','error');
		}
		if($postdata['mima']!=$basic['mima'].'123'){
			exi('输入的【操作密码】不对！','error');
		}
		$id=$postdata['id'];
		pdo_update('jy_goodserpfw_code',array('log_status'=>1),array('xiang_id'=>$postdata['id'],'uniacid'=>$_SESSION['uniacid']));
		pdo_update('jy_goodserpfw_xcode',array('log_status'=>1),array('id'=>$postdata['id'],'uniacid'=>$_SESSION['uniacid']));
		exi('操作成功！','error','/xiang/list');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择箱！','error');
	}
	$item=pdo_get('jy_goodserpfw_xcode',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'',
		'title'=>'恢复未出库状态',
		'edit'=>array(
			array('title'=>'【温馨提示】：本操作会同时更新本箱的所有盒标签【恢复未出库状态】状态','type'=>'tip'),
			array('name'=>'mima','title'=>'操作密码','type'=>'string','help'=>'为了安全，必须输入操作密码，才能操作'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
?>